/**
 * @(#) NoticeProxy.as
 *
 * @date		2009. 03. 11.
 * @version		1.0
 * @author		Park, JuneHo
 * @description	 공지사항 쓰기 - Proxy
 * 				공지사항 조회하는 Model Logic 
 */
package com.skt.common_hr.healthreq.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.healthreq.HealthreqFacade;
	import com.skt.common_hr.healthreq.model.vo.NoticeDetailVO;
	import com.skt.common_hr.healthreq.model.vo.NoticeFileUpLoadVO;
	import com.skt.common_hr.healthreq.model.vo.NoticeVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.AlertHelper;
	import com.wcl.utils.DateUtil;
	
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class NoticeWriteProxy extends CommonProxy
	{
		public static const NAME:String = "NoticeWriteProxy";
		
		public var writeKey:String;


		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function NoticeWriteProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function loadNoticeKey():void
		{
			var params:Object = new Object();
			params["I_NR_NAME"]  = "ZHRBOARD";
			params["I_BOARD_ID"] = "01";
			loadData("Z_HR_0054",params, loadKeyResult, loadKeyFault);
		}		
		

		public function insert(record:NoticeVO, sInsertType:String):void
		{
			var tables:Object 			= new Object();
			var table:Array = new Array();
			
			var params:Object 		= new Object();
			params["I_ACTIO"]		= "INS";
			params["I_PERNR"]		= Variables.myPernr;
			params["I_HENUM"]		= ""; // 신청시 빈값 . 
			
											
/* 			var itab:Object 				= new Object();		
			itab["HEGBN"]	= record.HEGBN;   	
			itab["MEDCK"]	= record.MEDCK;  
			itab["MEFAM"]	= record.MEFAM; 
			itab["MEFNM"]	= record.MEFNM; 
			itab["MEREG"]	= record.MEREG;  
			itab["EMESTO"]	= record.EMESTO;
			itab["STATE"]	= record.STATE;    
			itab["STCD2"]	= record.STCD2;    
			itab["MREDAT"]	= record.MREDAT;
			itab["MEDCD"]	= record.MEDCD;  
			itab["AMED01"]	= record.AMED01;
			itab["AMED02"]	= record.AMED02;
			itab["AMED03"]	= record.AMED03;
			itab["AMED04"]	= record.AMED04;
			itab["AMED05"]	= record.AMED05; */
			
			var itab:Object 				= new Object();		
			itab = record;
			table.push(itab);
			tables["ITAB"] = table;
			
			
			saveData("Z_HR_HE02", params, tables, insertResult, insertFault);
			
			//loadData("Z_HR_LE02", params, insertResult, insertFault);
		}


		public function update(record:NoticeVO, sInsertType:String):void
		{			
			var tables:Object 			= new Object();
			var table:Array = new Array();
			
			var params:Object 		= new Object();
			params["I_ACTIO"]		= "MOD";
			params["I_PERNR"]		= Variables.myPernr;
			params["I_HENUM"]		= record.HENUM;
			
											
			var itab:Object 	= new Object();		
			itab = record;
			table.push(itab);
			tables["ITAB"] = table;
			
			saveData("Z_HR_HE02", params, tables, updateResult, updateFault);
		}
		
		
		public function reply_insert(oReply:Object, sInsertType:String):void
		{
			var tables:Object 			= new Object();
			var table:Array = new Array();
			
			var params:Object 		= new Object();
			params["I_ACTIO"]		= "MOD";//sInsertType;
			params["I_PERNR"]		= Variables.myPernr;
			params["I_HLNUM"]		= oReply.HLNUM;
			
			var itab:Object 				= new Object();		
			itab["RCONTENT"]			= oReply.RCONTENT;		
			itab["RCONTX"]			= oReply.RCONTX;
			table.push(itab);
			tables["ITAB"] = table;
			
			
			saveData("Z_HR_LE02", params, tables, insertResult, insertFault);
			
			//loadData("Z_HR_LE02", params, insertResult, insertFault);
		}
		
		//파일정보업데이트
		public function fileInfoUpdate(record:NoticeDetailVO):void
		{
			CursorManager.setBusyCursor();
			
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0038", Variables.serverName);
			
			var params:Object = new Object();
			params["I_BOARD_ID"]		= record.I_BOARD_ID;
			params["I_CONTENT_IDX"]		= record.HLNUM;
			params["I_TITLE"]			= record.I_TITLE;
			params["I_CONTENT"]			= record.I_CONTENT;
			params["I_FILE_SIZE"]		= record.I_FILE_SIZE;
			params["I_FILE_NAME"]		= record.I_FILE_NAME;
			params["I_FILE_EXT"]		= record.I_FILE_EXT;
			params["I_FILE_URL"]		= record.I_FILE_URL;
			
			//loader.load(params, fileInfoResult, fileInfoFault);
		}

		public function fileUpload(fileInfo:NoticeFileUpLoadVO, table:Array):void
		{
			CursorManager.setBusyCursor();
			
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0036", Variables.serverName);
			
			var params:Object = new Object();
			//params["I_CONTENT_IDX"]		= fileInfo.I_CONTENT_IDX.toString();
			params["I_FILE_SIZE"]		= fileInfo.I_FILE_SIZE.toString();
			//params["I_FILE_NAME"]		= fileInfo.I_FILE_NAME;
			params["I_FILE_EXT"]		= fileInfo.I_FILE_EXT;
			//params["I_FILE"]			= fileInfo.I_FILE;
			var tables:Object = new Object();
			tables["BYTE_TAB"] = table;
			 
			//loader.load(params, fileInsertResult, fileInsertFault);
			//loader.save(params, tables, fileUploadResult, fileUploadFault); 
		}




		// 선택검진
		public function loadReserve_Medcd(MEDCK:String, STCD2:String, setFunc:Function):void
		{
			var params:Object = new Object();
			//params["I_PERNR"] 		= Variables.myPernr;
			params["I_MEDCK"] 		= MEDCK;
			params["I_STCD2"]		= STCD2;
			
			loadData("Z_HR_HE04", params, loadReserve_MedcdResult, listFault, setFunc); 
		}
		public function loadReserve_MedcdResult(event:ResultEvent):void
		{
			if(retCode==0) {
				//mainList = parseMainList(event.result.O_ITAB);
				event.result.func(event.result)
				trace("1");
			}
			else {
				Alert.show('loadListResult err: ' + retText);
			}
		}
		
		// 추가검진
		public function loadReserve(BEGDA:Date, STCD2:String, setFunc:Function):void
		{
			var params:Object = new Object();
			params["I_PERNR"] 		= Variables.myPernr;
			params["I_MREDAT"] 	= DateUtil.toBasicString(BEGDA);
			params["I_STCD2"]		= STCD2;
			
			loadData("Z_HR_HE03", params, loadReserveResult, listFault, setFunc); 
		}
		public function loadReserveResult(event:ResultEvent):void
		{
			if(retCode==0) {
				//mainList = parseMainList(event.result.O_ITAB);
				event.result.func(event.result)
				trace("1");
			}
			else {
				Alert.show('loadListResult err: ' + retText);
			}
		}


		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}		










		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function loadKeyResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				writeKey = event.result["O_CONTENT_IDX"];
				facade.sendNotification(HealthreqFacade.LOAD_KEY_COMPLETE, this);
			}
			else {
				facade.sendNotification(HealthreqFacade.LOAD_KEY_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function loadKeyFault(event:FaultEvent):void
		{
			facade.sendNotification(HealthreqFacade.LOAD_KEY_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		

		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function insertResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				facade.sendNotification(HealthreqFacade.INSERT_NOTICE_COMPLETE, this);
			}
			else {
				trace( "failed to insert article: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				AlertHelper.showMessage(event.result["RETTEXT"], "오류");
				facade.sendNotification(HealthreqFacade.INSERT_NOTICE_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}
		

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function insertFault(event:FaultEvent):void
		{
			trace("Fault message in CertificateProxy: " + event.fault.faultString);
			facade.sendNotification(HealthreqFacade.INSERT_NOTICE_FAILED, this);
			CursorManager.removeBusyCursor();
		}


		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function updateResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				facade.sendNotification(HealthreqFacade.UPDATE_NOTICE_COMPLETE, this);
			}
			else {
				trace(event.result["RETCODE"]+" : "+event.result["RETTEXT"]);
				AlertHelper.showMessage(event.result["RETTEXT"], "오류");
				facade.sendNotification(HealthreqFacade.INSERT_NOTICE_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function updateFault(event:FaultEvent):void
		{
			facade.sendNotification(HealthreqFacade.UPDATE_NOTICE_FAILED, this);
			CursorManager.removeBusyCursor();
		}
		
		public function fileInfoResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {
				//facade.sendNotification(NoticeFacade.FILEINFO_UPDATE_NOTICE_COMPLETE, this);
			}
			else {
				trace(event.result["RETCODE"]+" : "+event.result["RETTEXT"]);
				//facade.sendNotification(NoticeFacade.FILEINFO_UPDATE_NOTICE_FAILED, this);
			}
			CursorManager.removeBusyCursor();
		}

		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fileInfoFault(event:FaultEvent):void
		{
			//facade.sendNotification(NoticeFacade.FILEINFO_UPDATE_NOTICE_FAILED, this);
			CursorManager.removeBusyCursor();
		}





		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
	}
}